<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <button @click="showStore">显示仓库的内容</button>
        <h1>{{this.$store.state.counter}}</h1>
        <h1>总价 {{this.$store.getters.totalPrice}}</h1>
        <button @click="add">加1</button>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vuex/dist/vuex.js"></script>
<script src="./node_modules/axios/dist/axios.js"></script>
<script>
    let store = new Vuex.Store({
        getters: {
            totalPrice: (state) => {
                console.log(this);
                return state.counter * 10;
            }
        },
        state: {
            counter: 888
        },
        mutations: {
            increment: function(state, count) {
                state.counter += count;
            }
        },
        actions: {
            asyncIncrement: function(store) {
                axios.get("http://linweiqin.cn:8001/statis/admin/2020-10-17/count").then(res => {
                    console.log(res.data.count);
                    store.commit("increment", res.data.count)
                }).catch(err => console.log(err))
            }
        }
    })
    let vm = new Vue({
        store,
        el: "#app",
        methods: {
            showStore: function() {
                console.log(this.$store);
            },
            add: function() {
                this.$store.dispatch("asyncIncrement");
            }
        },
    })
</script>

</html>